package list;

import java.util.Iterator;

/**
 * description:
 * author:张腾
 * date:2021-03-31
 */
public class TwoWayLinkList<T> implements Iterable<T>{
    //记录头节点
    private Node head;
    //最后一个节点
    private Node last;
    //记录链表长度
    private int N;



    //节点类
    private class Node{
        //存储数据
        T item;
        //下一个节点
        Node next;
        //上一个节点
        Node pre;

        public Node(T item,Node pre , Node next){
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
    }

    public TwoWayLinkList(){
        this.head = new Node(null,null,null);
        this.last = null;
        this.N = 0;
    }

    public void clear(){
        this.head.next = null;
        this.head.pre = null;
        this.head.item = null;
        this.last = null;
        this.N = 0;
    }

    //获取第一个元素
    public T getFirst(){
        if (isEmpty()){
            return null;
        }
        return head.next.item;
    }

    //获取最后一个元素
    public T getLast(){
        if (isEmpty()){
            return null;
        }
        return last.item;
    }

    public boolean isEmpty(){
        return this.N==0;
    }

    //获取线性表的长度
    public int length(){
        return N;
    }

    //获取指定位置的元素
    public T get(int i){
        Node n = head.next;
        for(int index = 0;index < i;index ++){
            n = n.next;
        }
        return n.item;
    }

    //向线性表添加元素t
    public void insert(T t){
        //如果链表为空
        if (isEmpty()){
            Node newNode = new Node(t,head,null);
            last = newNode;
            head.next = last;
        }else {
            Node oldLast = last;
            Node newNode = new Node(t, oldLast, null);
            oldLast.next = newNode;
            last = newNode;
        }
        N++;
    }

    //向线性表指定i位置添加元素
    public void insert(int i,T t){
        Node pre = head;
        for(int index = 0;index <= i-1;index ++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node newNode = new Node(t, pre, curr);
        pre.next = newNode;
        curr.pre = newNode;
        N++;
    }

    //删除指定位置的元素，并返回该元素
    public T remove(int i){
        Node pre = head;
        //找到i位置前一个结点
        for(int index = 0;index <= i-1;index ++){
            pre = pre.next;
        }
        Node curr = pre.next;
        Node nextNode = curr.next;
        pre.next = nextNode;
        nextNode.pre = pre;
        N--;
        return curr.item;
    }

    //查找t元素第一次出现的位置
    public int indexOf(T t){
        Node n = head;
        for (int i = 0;n.next!=null;i ++){
            n = n.next;
            if (n.item.equals(t)){
                return i;
            }
        }
        return -1;
    }


    @Override
    public Iterator iterator() {
        return new TIterator()  ;
    }

    private class TIterator implements Iterator{
        private Node n;
        public TIterator(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}
